# Friends_Bet controller controls friend betting actions
# In 13wins, the user may view bets placed by friends and also wager challenges against them using WinPoints

class FriendsBetController < ApplicationController

  # List challenges using match id and user id
  def index
    @sid = params[:sid]
    metch = Match.find(@sid)
    uid = params[:fb_sig_user]
    @user = User.find(uid)
    @home_friends = metch.bets.home_friends(@user.all_friends).map{|b| b.user}
    @away_friends = metch.bets.away_friends(@user.all_friends).map{|b| b.user}
    @other_friends = User.find(:all,:conditions=>"id in (#{@user.all_friends})",:order=>'name') - @home_friends - @away_friends
    render :partial=>'index',:locals=>{:sid=>@sid,:user=>@user}
  end

  # Display sent challenges and notify via Facebook Notification and Email
  # Mark the challenge status to "bet_0"
  def sent
     place_amount = params[:amount].to_i > 500 ? 500 : params[:amount].to_i
    if User.find(params[:fb_sig_user]).point > place_amount and !Challenge.find(:first,:conditions=>["from_user_id=? and to_user_id =? and match_id = ?",params[:fb_sig_user],params[:to_user_id],params[:sid]])
      @to_user = User.find(params[:to_user_id])
      ch = Challenge.new
      ch.from_user_id = params[:fb_sig_user]
      ch.to_user_id   = params[:to_user_id]
      ch.amount       = place_amount
      ch.message      = params[:message]
      ch.match_id     = params[:sid]
      ch.mode         = "bet_0"
      ch.save

      ch.sender.point = ch.sender.point - ch.amount.to_i 
      ch.sender.save
      PointHistory.create(:user_id=>ch.sender.id,:action=>'Wagered a challenge on ' + ch.match.info,:point=>ch.amount,:earning => false)

      NotificationPublisher.deliver_challenge_notify(ch)
      NotificationPublisher.deliver_challenge_email(ch)
      @error = nil
    else
      @error = "You do not have enough WinPoints to make this challenge or have already challenged this friend."
    end
    render :partial=>'sent'
  end

#"/friends_bet/gen_dialog?sid=" + match.id.to_s + "&uid=" + ch.sender.id.to_s + "&chid=" + ch.id.to_s
  # Generate a challenge and send it via match id and user id
  def gen_dialog
    sid= params[:sid]
    uid = params[:fb_sig_user]
    u = nil
    u = User.find(params[:uid]) if params[:uid]
    ch = Challenge.find(params[:chid]) if params[:chid]
    metch = Match.find(sid)
    @user = User.find(uid)
    @home_friends = metch.bets.home_friends(@user.all_friends).map{|b| b.user}
    @away_friends = metch.bets.away_friends(@user.all_friends).map{|b| b.user}
    @ched_friends = Challenge.find(:all,:conditions=>["from_user_id=? and match_id = ?",params[:fb_sig_user],params[:sid]]).map{|ch| User.find(ch.to_user_id)}
    @other_friends = User.find(:all,:conditions=>"id in (#{@user.all_friends})",:order=>'name') - @home_friends - @away_friends - @ched_friends
    if ch
      render :partial=>'place_challenge_form',:locals=>{:sid=>sid,:u=>u,:ch=>ch}
    else
      render :partial=>'place_challenge_form',:locals=>{:sid=>sid,:u=>u}
    end
  end

  # Accept a challenge action
  # Mark the challenge status to "bet_1"
  def accept
    ch =  Challenge.find(params[:chid])
    @error = nil
    if ch.goter.point > ch.amount.to_i 
      ch.mode = "bet_1"
      ch.save
      ch.goter.point = ch.goter.point - ch.amount 
      ch.goter.save
      unless PointHistory.exists?(["user_id =? and action=? and point = ?",current_user.id,'Accept challenge bet winpoints ' + ch.match.info,ch.amount])
         PointHistory.create(:user_id=>ch.goter.id,:action=>'Accepted a challenge: ' + ch.match.info,:point=>ch.amount,:earning => false)
      end

    else
      @error = "Not enough WinPoints to accept this challenge."
    end
    render :partial=>'accept'
  end
  # all sent challenges 
  def all
    @chs  = current_user.sent_challenges
  end
end
